Skip to content

feat(pulse): optimize gas while keeping requests on-chain #2519

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Apr 2, 2025

Conversation

tejasbadadare
Copy link
Contributor

@tejasbadadare tejasbadadare commented Mar 24, 2025

Summary

Gas optimizations to the Pulse contract that keep the requests on-chain.

Rationale

The motivation behind this PR is to see how cheap we can get the basic request/fulfill flow while still keeping full request state on-chain. This will let us treat the contract as the source of truth for state, greatly simplifying keeper implementation. It also eliminates a class of errors related to tracking chain state, improving reliability.

This is an alternative design to the gas optimizations done in #2492 , which make the opposite tradeoff: move request state off-chain (specifically the priceIds array) to gain gas savings at the cost of keeper complexity.

Results:
https://docs.google.com/spreadsheets/d/1AYcmnwDQp2OA8n0xfoTf-GtThmhsX1ahxMQilIua4b0/edit?gid=0#gid=0

Optimizations made:

  • Make the priceIds array dynamically sized rather than pre-allocated to size MAX_PRICE_IDS. This makes the gas price dynamic based on the request, but it will still be predictable. The assumption here is that most users will use much fewer than the maximum price IDs in a single request.
  • Only store an 8-byte prefix of the price IDs in the request. This takes the array from bytes32[MAX_PRICE_IDS] to bytes8[]. There is very low chance of collision at 8 bytes, and even at 4 bytes, so we can go even lower here.
    • Assuming the hash function gives uniformly random output, the first 64 bits (8 bytes) gives 2^64 possible values. Using the birthday approximation P ~= (n(n-1))/(2 * 2^64) (where P is the chance of a collision for n entries) gives 2.7e-12, which is very unlikely.
  • Reduce datatype sizes for uints to more reasonable sizes. This saves a few storage slots in the state structs. We could go even lower on some of these, but perhaps I'm missing some context on why we chose such high numbers here.
    • Changed callbackGasLimit from uint256 to uint32, since this number can't exceed the block gas limit anyway
    • Changed exclusivityPeriod from uint256 to uint32, since we only support requests 60s into the future, and 32 bytes still lets us represent ~49k days in seconds.
    • Changed pythFeeInWei, baseFeeInWei, feePerFeedInWei, feePerGasInWei from uint128 to uint96 since 96 bytes still gives us the ability to represent up to ~79,228,162,514 ETH in wei. We could go lower here.

Notes:

  • We will need to keep a mapping of prefix -> price feed ID in the keeper, since we can't directly query Hermes with just a prefix. That means we'll need to keep the price feed ID set up to date somehow (on demand when querying for a price? on a timer?)
  • We can also further optimize the firstUnfulfilledSeq feature, as called out in the code:
// TODO: I'm pretty sure this is going to use a lot of gas because it's doing a storage lookup for each sequence number.
// a better solution would be a doubly-linked list of active requests.
  • Current tests cover my changes
  • Added new tests
  • Manually tested the code

Copy link

vercel bot commented Mar 24, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
component-library ✅ Ready (Inspect) Visit Preview 💬 Add feedback Mar 27, 2025 9:46pm
insights ✅ Ready (Inspect) Visit Preview 💬 Add feedback Mar 27, 2025 9:46pm
4 Skipped Deployments
Name Status Preview Comments Updated (UTC)
api-reference ⬜️ Ignored (Inspect) Visit Preview Mar 27, 2025 9:46pm
entropy-debugger ⬜️ Ignored (Inspect) Visit Preview Mar 27, 2025 9:46pm
proposals ⬜️ Ignored (Inspect) Visit Preview Mar 27, 2025 9:46pm
staking ⬜️ Ignored (Inspect) Visit Preview Mar 27, 2025 9:46pm

Copy link
Collaborator

@m30m m30m left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm. Can we expand the benchmark a bit? Include 2-3 different scenarios like:

1- Making a request for 1-2-4-8 different feeds
2- Filling each of these requests

}

struct ProviderInfo {
uint128 baseFeeInWei;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why this particular ordering if we can pack it even more?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I dont think we can do better than 3 slots for ProviderInfo unless i'm missing something?

@tejasbadadare
Copy link
Contributor Author

@m30m @ali-bahjati round 2!

Expanded the gas benchmarks:

  • Gas cost of overflowing the requests array and need to store in the overflow mapping, for a range of feeds.
  • Gas cost of a worst-case out-of-order fulfillment. The last fulfillment will be the most expensive since it needs to linearly scan through all the fulfilled requests in storage in order to update _state.lastUnfulfilledReq expensive update of _state.lastUnfulfilledSequence
    • This will be useful if and when we optimize the lastUnfulfilledReq feature (e.g. via doubly linked list)

Updated the Pyth gas benchmarks as well.

Copy link
Collaborator

@ali-bahjati ali-bahjati left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@tejasbadadare tejasbadadare merged commit f922acb into main Apr 2, 2025
10 checks passed
@tejasbadadare tejasbadadare deleted the tb/pulse/gas-opt-2 branch April 2, 2025 22:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants